package com.orcwar.ai;

import java.util.List;

import com.orcwar.engine.IOWGameListener;
import com.orcwar.engine.OWGame;
import com.orcwar.engine.OWMap;
import com.orcwar.engine.OWPlayer;
import com.orcwar.engine.OWTile;
import com.orcwar.engine.OWUnit;

/**
 * Interface for Artificial Intelligences in OrcWar.
 * 
 * @author fbastien
 */
public abstract class OWAI implements IOWGameListener, Runnable
{
	/** The game engine from which to retrieve data and execute actions. */
	protected final OWGame game;
	/** The player that this AI plays for. */
	protected final OWPlayer player;
	
	/**
	 * Initializes the AI.
	 * 
	 * @param game the game engine.
	 * @param player the player that this AI plays for.
	 */
	public OWAI(OWGame game, OWPlayer player)
	{
		this.game = game;
		this.player = player;
	}
	
	@Override
	public final void run()
	{
		doTurn();
	}
	
	/**
	 * Handles the enemy turn.
	 */
	public abstract void doTurn();

	/**
	 * {@inheritDoc}
	 * 
	 * Does nothing by default.
	 */
	@Override
	public void onLevelStart(OWMap map)
	{}

	/**
	 * {@inheritDoc}
	 * 
	 * Does nothing by default.
	 */
	@Override
	public void onLevelEnd(OWMap map, OWPlayer winner)
	{}

	/**
	 * {@inheritDoc}
	 * 
	 * By default, returns itself as the callback if it's the AI's turn, or does
	 * nothing if it's not its turn.
	 * 
	 * @see #run()
	 * @see #doTurn()
	 */
	@Override
	public Runnable onTurnStart(OWPlayer currentPlayer, int turn)
	{
		if(currentPlayer.equals(player))
		{
			return this;
		}
		else
		{
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * Does nothing by default.
	 */
	@Override
	public void onTurnEnd(OWPlayer currentPlayer)
	{}

	/**
	 * {@inheritDoc}
	 * 
	 * Does nothing by default.
	 */
	@Override
	public void onUnitMove(OWUnit unit, OWTile startTile, OWTile endTile,
			List<OWTile> path)
	{}

	/**
	 * {@inheritDoc}
	 * 
	 * Does nothing by default.
	 */
	@Override
	public void onUnitAttack(OWUnit unit, OWTile attackedTile, boolean success, List<OWUnit> attackedUnit)
	{}

	/**
	 * {@inheritDoc}
	 * 
	 * Does nothing by default.
	 */
	@Override
	public void onUnitDeath(OWUnit unit)
	{}
	
	/**
	 * {@inheritDoc}
	 * 
	 * Does nothing by default.
	 */
	@Override
	public void onUnitTurnEnd(OWUnit unit)
	{}
}
